<?php
class mysql
{
    private $mysqli;
    private $result;
    /**
     * 数据库连接
     */
    public function connect($config)
    {
        $host = $config['host'];        //主机地址
        $username = $config['username'];//用户名
        $password = $config['password'];//密码
        $dbname = $config['dbname'];    //数据库
        // $port = $config['port'];     //端口号
        $this->mysqli = mysqli_connect($host, $username, $password, $dbname);
        // 检测连接
        if (!$this->mysqli) {
            die("Connection failed: " . mysqli_connect_error() . "<br />");
        }
        // 设置编码
        mysqli_query($this->mysqli, "set names utf8");
    }

    /**
     * 数据查询
     * 
     * 返回查询的结果
     */
    public function select($table, $field = null, $where = null)
    {
        // 拼接数据表数组为字符串
        $table = implode(',', $table);
        $sql = "SELECT * FROM {$table}";
        if (!empty($field)) {
            // 拼接字段数组为字符串
            $field = implode(',', $field);
            $sql = str_replace('*', $field, $sql);
        }
        if (!empty($where)) {
            $sql = $sql . ' WHERE ' . $where;
        }
        $this->result = mysqli_query($this->mysqli, $sql);
        if (!$this->result) {
            die('ERROR: ' . mysqli_error($this->mysqli));
        } else {
            return $this->result;
        }
    }

    /**
     * 转换结果为数组
     */
    public function transform($data)
    {
        $this->result = mysqli_fetch_assoc($data);
        return $this->result;
    }

    /**
     * 插入数据
     * 返回插入的 id
     */
    public function insert($table, $data)
    {
        foreach ($data as $key => $value) {
            // 转义 SQL 语句中使用的字符串中的特殊字符
            $data[$key] = mysqli_real_escape_string($this->mysqli, $value);
        }
        $keys = implode(",", array_keys($data));
        $values = "'" . implode("','", array_values($data)) . "'";
        $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";
        $this->result = mysqli_query($this->mysqli, $sql);
        $_id = mysqli_insert_id($this->mysqli);
        if (!$this->result) {
            die('ERROR: ' . mysqli_error($this->mysqli));
        } else {
            echo '插入数据成功！插入数据的 id 是：' . $_id .'<br/>';
            return $_id;
        }
    }
    /**
     * 更新数据
     * 返回 $row 改变的记录数
     */
    public function update($table, $data, $where)
    {
        foreach ($data as $key => $value) {
            $data[$key] = mysqli_real_escape_string($this->mysqli, $value);
        }
        $sets = array();
        foreach ($data as $key => $value) {
            $sets[] = $key . "=" . "'" . $value . "'";
        }
        $kav = implode(",", $sets);
        $sql = "UPDATE {$table} SET {$kav} WHERE {$where}";
        $retval = mysqli_query($this->mysqli, $sql);
        $row = mysqli_affected_rows($this->mysqli);
        if(!$retval) {
            die('ERROR: ' . mysqli_error($this->mysqli));
        } else {
            echo '数据更新成功！已更新' . $row .'行数据。<br/>';
            return $row;
        }
    }

    /**
     * 删除数据
     * 返回 $row 改变的记录数
     */
    public function delete($table, $where)
    {
        $sql = "DELETE FROM {$table} WHERE {$where}";
        $retval = mysqli_query($this->mysqli, $sql);
        if ($retval) {
            $row = mysqli_affected_rows($this->mysqli);
            echo '删除数据成功！删除了' . $row . '行数据。<br />';
            return $row;
        } else {
            die('ERROR: ' . mysqli_error($this->mysqli));
        }
    }
}